// history模式

// createWebHashHistory hash模式
// createWebHistory  history模式
import {
    createRouter,
    createWebHistory,
} from 'vue-router'

const routes = [
    // 路由的默认路径
    {
        path: '/',
        // 重定向
        redirect: "/login"
    },
    {
        path: '/login',
        name: 'login',
        component: () =>
            import('../components/login.vue')
    },
    {
        path: '/home',
        name: 'home',
        component: () =>
            import('../components/home.vue'),
        // 嵌套路由
        children: [
            {
                path: '/about',
                name: 'about',
                component: () =>
                    import('../components/about.vue')
            },
            {
                path: '/homepage',
                name: 'homepage',
                component: () =>
                    import('../components/homepage.vue'),
            }, {
                path: '/personal',
                name: 'personal',
                component: () =>
                    import('../components/personal.vue')
            },
            {
                path: '/details',
                name: 'details',
                component: () =>
                    import('../components/details.vue')
            }
        ]
    },

]

// 创建路由对象
const router = createRouter({
    history: createWebHistory(),

    routes
})
router.beforeEach((to, form, next) => {
    // next()
    let UserName = localStorage.getItem("用户名")
    let PassWord = localStorage.getItem('密码')
    if (to.path == '/login') {
        if (UserName && PassWord) {
            next({ name: 'homepage' })
        } else {
            next()
        }
    } else {
        if (UserName && PassWord) {
            next()
        } else {
            next({ name: 'login' })
        }
    }
})
// 后置守卫
router.afterEach((to, from) => {
    // 改变头部的地址变为当前路由名
    document.title = to.name
})
export default router;